package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class HuanbohainewsNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.huanbohainews.com.cn/system/[0-9]+/[0-9]+/[0-9]+/[0-9]+.shtml"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if(url.startsWith("http://photo.")){
			title =doc.select("span.yanse_bai.heiti20").text();
			if(StringUtils.isBlank(title)){
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("span.yanse_bai p"));
			String text=doc.select("span.p12.yanse_hui").text();
			date=DateUtils.matchDate(text);
			news.setAuthor(StringUtils.substringAfter(text, "来源：").replace(" ", ""));
		}else{
			title =doc.select("div[align=center] h1").text();
			if(StringUtils.isBlank(title)){
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("td.p14 p"));
			date=DateUtils.matchDate(doc.select("td.STYLE2").text());
			String text=doc.select(".STYLE2:containsOwn(来源：)").text();
			if(StringUtils.isNotBlank(text)){
				news.setAuthor(StringUtils.substringAfter(text, "来源：").replace(" ", ""));
			}
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		HuanbohainewsNewsAnalyse analyse=new HuanbohainewsNewsAnalyse();
		String url="http://photo.huanbohainews.com.cn/system/2014/01/03/011299542.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
